package com.example.myapplication.module

import com.example.myapplication.CurrentCallback
import com.example.myapplication.MyApplication
import com.example.myapplication.entity.LoginBean
import com.example.myapplication.soaprequest.CustomerObserverSoap
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.mylibrary.base.BasePresenterImpl
import com.example.mylibrary.network.BaseData
import com.example.mylibrary.network.RetrofitUtil
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.SpUtils
import com.google.gson.reflect.TypeToken
import io.reactivex.disposables.Disposable

class LoginPresenter : BasePresenterImpl<LoginActivity>() {

    fun getAccountInfo(soapEnvelop: SoapEnvelop) {
        RetrofitUtil.initObservableCall(MyApplication.createService.soapRequest(soapEnvelop),
            object : CustomerObserverSoap {
                override fun onSubscribe(d: Disposable) {
                    mView?.loadPopup?.show()
                }

                override fun onComplete() {
                    mView?.loadPopup?.smartDismiss()
                }

                override fun onSuccess(result: String) {
                    mView?.setData(result)
                }

                override fun onError(e: Throwable) {
                    mView?.loadPopup?.smartDismiss()
                    super.onError(e)
                }
            })
    }

    // 查询手机是否注册
    fun getMobileReg(params: Map<String, String>) {
        RetrofitUtil.initCall(MyApplication.createService.getMobileClient(params),
            object : CurrentCallback<BaseData>() {
                override fun onSuccess(body: BaseData) {
                    mView?.setMobileSuccess()
                }
            })
    }

    // 获取信息
    fun getAccsetsaas(soapEnvelop: SoapEnvelop) {
        RetrofitUtil.initObservableCall(MyApplication.createService.soapRequest(
            soapEnvelop
        ),
            object : CustomerObserverSoap {
                override fun onSuccess(result: String) {
                    // TODO: 2020-11-06 公有云请求,结果暂时未处理
                }
            })
    }

    // 登录
    fun mobileReg(soapRequestEnvelope: SoapEnvelop) {
        RetrofitUtil.initObservableCall(MyApplication.createService.soapRequest(
            soapRequestEnvelope
        ),
            object : CustomerObserverSoap {
                override fun onSuccess(result: String) {
                    mView?.mobileRegSuccess()
                }
            })
    }


    // 登录
    fun login(soapRequestEnvelope: SoapEnvelop) {
        RetrofitUtil.initObservableCall(MyApplication.createService.soapRequest(soapRequestEnvelope),
            object : CustomerObserverSoap {
                override fun onSuccess(result: String) {
                    // 登录保存登录后信息
                    if (result.isNotEmpty()) {
                        val loginBean = GsonUtil.fromJson<List<LoginBean>>(
                            result,
                            object : TypeToken<List<LoginBean>>() {}.type
                        )
                        SpUtils.put("loginInfo", result)
                        SpUtils.put("opId", loginBean[0].id)
                        SpUtils.put("opName", loginBean[0].opname)
                    }
                    mView?.loginSuccess()
                }

                override fun onSubscribe(d: Disposable) {
                    mView?.loadPopup?.show()
                }

                override fun onComplete() {
                    mView?.loadPopup?.smartDismiss()
                }

                override fun onError(e: Throwable) {
                    mView?.loadPopup?.smartDismiss()
                    super.onError(e)
                }
            })
    }
}
